package com.sys.orders.back.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import com.alibaba.fastjson.JSONObject;
import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.goods.pojo.AAddress;
import com.sys.goods.pojo.ABlindbox;
import com.sys.goods.pojo.ABlindboxEnum;
import com.sys.goods.pojo.AGoods;
import com.sys.goods.service.IGoodsDbService;
import com.sys.users.pojo.AUsers;
import com.sys.users.service.IUsersDbService;


@Controller
@RequestMapping(value = "/back/goods",produces = "text/html;charset=UTF-8")
@MultipartConfig
public class GoodsBackController extends BaseController
{
	@Autowired
	private IGoodsDbService goodsDbService;
	
	@Autowired
	private  IUsersDbService usersDbService;
	
	/**
	 * 商品列表
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/goodsList")
	public String goodsList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		//this.path = "/back/goods/goodsList";
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);

		String blindBoxId = request.getParameter("blindBoxId");
		if (blindBoxId == null)
		{
			blindBoxId = "";
		}
		request.setAttribute("blindBoxId", blindBoxId);
		condMap.put("blindBoxId", blindBoxId);

//		String dataType = request.getParameter("dataType");
//		if (dataType == null)
//		{
//			dataType = "";
//		}
//		request.setAttribute("dataType", dataType);
//		condMap.put("dataType", dataType);

		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<AGoods> apiResponse = this.goodsDbService.findCondListAGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());

		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", ABlindboxEnum.STATUS_ENABLE.getStatus());

		ApiResponse<ABlindbox> boxApiResponse = this.goodsDbService.findCondListABlindboxService(null, condMap);
		request.setAttribute("boxResponse", boxApiResponse.toJSON());
		//this.serverRedirect(request, response, path);
		return "/back/goods/goodsList";
	}
	
	/**
	 * 商品添加
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/goodsInsert")
	public String goodsInsert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", ABlindboxEnum.STATUS_ENABLE.getStatus());
		ApiResponse<ABlindbox> boxResponse = this.goodsDbService.findCondListABlindboxService(null, condMap);
		request.setAttribute("boxResponse", boxResponse.toJSON());
		return "/back/goods/goodsInsert";
	}
	
	/**
	 * 商品添加提交
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/goodsInsertSubmit")
	@ResponseBody
	public String goodsInsertSubmit(HttpServletRequest request, HttpServletResponse response,AGoods goods)
			throws ServletException, IOException
	{

		/* 保存数据库 */
		ApiResponse<Object> apiResponse = this.goodsDbService.saveOneAGoodsService(goods);
		request.setAttribute("response", apiResponse.toJSON());
		/* 成功,提示 */
		JSONObject resultJSON = apiResponse.toJSON();
		return resultJSON.toJSONString();
	}
	
	/**
	 * 商品更新
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/goodsUpdate")
	public String goodsUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("goodsId", id);
//		condMap.put("extend", "true");
		ApiResponse<AGoods> apiResponse = this.goodsDbService.findOneAGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if ("update".equalsIgnoreCase(operType))
		{
			
			condMap.clear();
			condMap.put("status", ABlindboxEnum.STATUS_ENABLE.getStatus());
			ApiResponse<ABlindbox> boxApiResponse = this.goodsDbService.findCondListABlindboxService(null, condMap);
			request.setAttribute("boxResponse", boxApiResponse.toJSON());
			
//			request.setAttribute("operType", operType);
			
			return "/back/goods/goodsUpdate";
		}else if ("Photo".equalsIgnoreCase(operType))
		{
			return "/back/goods/goodsUpdatePhoto";
		}
		return "/back/goods/goodsInfo";
	}
	
	/**
	 * 商品更新提交
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/goodsUpdateSubmit")
	@ResponseBody
	public String goodsUpdateSubmit(HttpServletRequest request,Model model, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("goodsId", id);
		ApiResponse<AGoods> apiResponse = this.goodsDbService.findOneAGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		AGoods goods = apiResponse.getDataOneJava();

		boolean flag = false;
		String operType = request.getParameter("operType");
		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if ("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			String blindBoxId = request.getParameter("blindBoxId");
			String goodsName = request.getParameter("goodsName");
			String goodsPrices = request.getParameter("goodsPrices");
			String goodsIntroduce = request.getParameter("goodsIntroduce");
			String goodsPicture = request.getParameter("goodsPicture");
			String goodsQuantity = request.getParameter("goodsQuantity");
			String commodityBrand = request.getParameter("commodityBrand");
			String status = request.getParameter("status");

			String pubTimeStr = request.getParameter("pubTimeStr");
			String shelfTime = request.getParameter("shelfTime");

			/* 赋值 */
			goods.setBlindBoxId(Integer.valueOf(blindBoxId));
			goods.setGoodsName(goodsName);
			goods.setGoodsPrices(Integer.valueOf(goodsPrices));
			goods.setGoodsIntroduce(goodsIntroduce);
			goods.setGoodsPicture(goodsPicture);
			goods.setGoodsQuantity(Integer.valueOf(goodsQuantity));
			goods.setCommodityBrand(commodityBrand);
			goods.setStatus(Byte.valueOf(status));
			

			flag = true;
		}else if("Photo".equalsIgnoreCase(operType))
		{
			/* 上传图片 */
			try
			{
				/* 更新标志 */
				if(request instanceof MultipartRequest)
				{ 
					MultipartRequest multipartRequest = (MultipartRequest) request ;
					MultipartFile multipartFile = multipartRequest.getFile("file");
					condMap.put("fileName", multipartFile.getOriginalFilename());
					condMap.put("is", multipartFile.getInputStream());
					goods.setUpdateTime(new Date());
					apiResponseDb = this.goodsDbService.updateOneAGoodsService(goods, condMap);
					model.addAttribute("response", apiResponseDb.toJSON());
					request.getSession().setAttribute("goods", goods);
				}
			} catch (Exception e)
			{
				ConstatFinalUtil.SYS_LOGGER.error("更新图片失败了;" , e );
			}
		}
		

		
		if (flag)
		{
			apiResponseDb = this.goodsDbService.updateOneAGoodsService(goods);
		}

		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		return resultJSON.toJSONString();
	}
	
	
	/* ==============================box=================================== */
	/**
	 * 盲盒列表
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/boxList")
	public String boxList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		//this.path = "/back/goods/goodsList";
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);

		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<ABlindbox> apiResponse = this.goodsDbService.findCondListABlindboxService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());

		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", ABlindboxEnum.STATUS_ENABLE.getStatus());
		
		
		return "/back/goods/boxList";
	}
	
	/**
	 * 盲盒添加
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/boxInsert")
	public String boxInsert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", ABlindboxEnum.STATUS_ENABLE.getStatus());
		
		return "/back/goods/boxInsert";
	}
	
	/**
	 * 盲盒添加提交
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/boxInsertSubmit")
	@ResponseBody
	public String boxInsertSubmit(HttpServletRequest request, HttpServletResponse response,ABlindbox box)
			throws ServletException, IOException
	{
//		String pubTimeStr = request.getParameter("pubTimeStr");
//		box.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));

		/* 保存数据库 */
		ApiResponse<Object> apiResponse = this.goodsDbService.saveOneABlindboxService(box);

		request.setAttribute("response", apiResponse.toJSON());
		/* 成功,提示 */
		JSONObject resultJSON = apiResponse.toJSON();
		return resultJSON.toJSONString();
	}
	
	/**
	 * 盲盒更新
	 * 
	 * @param request
	 * @param response  
	 */
	@RequestMapping("/boxUpdate")
	public String boxUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("blindBoxId", id);
//		condMap.put("extend", "true");
		ApiResponse<ABlindbox> apiResponse = this.goodsDbService.findOneABlindboxService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if ("update".equalsIgnoreCase(operType))
		{
			
			condMap.clear();
			condMap.put("status", ABlindboxEnum.STATUS_ENABLE.getStatus());
//			request.setParameter("operType",operType);
			return "/back/goods/boxUpdate";
		}else if ("Photo".equalsIgnoreCase(operType))
		{
			return "/back/goods/boxUpdatePhoto";
		}
		return "/back/goods/boxInfo";
	}
	
	/**
	 * 盲盒更新提交
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/boxUpdateSubmit")
	@ResponseBody
	public String boxUpdateSubmit(HttpServletRequest request,Model model, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("blindBoxId", id);
		
		 ApiResponse<ABlindbox> apiResponse = this.goodsDbService.findOneABlindboxService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		 ABlindbox box = apiResponse.getDataOneJava();

		boolean flag = false;
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			String blindBoxNumber = request.getParameter("blindBoxNumber");
			String blindBoxPrices = request.getParameter("blindBoxPrices");
			String blindBoxIntroduce = request.getParameter("blindBoxIntroduce");
			String status = request.getParameter("status");
			
			/* 赋值 */
			box.setBlindBoxNumber(Integer.valueOf(blindBoxNumber));
			box.setBlindBoxPicture(blindBoxPrices);
			box.setBlindBoxIntroduce(blindBoxIntroduce);
			box.setStatus(Byte.valueOf(status));
			


			flag = true;
		}else if("Photo".equalsIgnoreCase(operType))
		{
			/* 上传图片 */
			try
			{
				/* 更新标志 */
				if(request instanceof MultipartRequest)
				{ 
					MultipartRequest multipartRequest = (MultipartRequest) request ;
					MultipartFile multipartFile = multipartRequest.getFile("file");
					condMap.put("fileName", multipartFile.getOriginalFilename());
					condMap.put("is", multipartFile.getInputStream());
					box.setUpdateTime(new Date());
					ApiResponse<Object> dbResponse = this.goodsDbService.updateOneABlindboxService(box, condMap);
					model.addAttribute("response", dbResponse.toJSON());
					request.getSession().setAttribute("box", box);
					return dbResponse.toJSON().toJSONString();
				}
			} catch (Exception e)
			{
				ConstatFinalUtil.SYS_LOGGER.error("更新图片失败了;" , e );
			}
		}
		

		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if (flag)
		{
			 apiResponseDb = this.goodsDbService.updateOneABlindboxService(box);
		}

		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		return resultJSON.toJSONString();
	}
	
	/* ==============================address=================================== */
	/**
	 * 地址列表
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/addressList")
	public String addressList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		//this.path = "/back/goods/addressList";
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);

		/*
		 * 查询数据库 所有的数据以JSON的形式返回
		 */
		ApiResponse<AAddress> apiResponse = this.goodsDbService.findCondListAAddressService(pageInfoUtil, condMap);

		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());

		/* 查询所有的分类;只查询商品的分类 */
		condMap.clear();
		condMap.put("status", ABlindboxEnum.STATUS_ENABLE.getStatus());
		
		
		return "/back/goods/addressList";
	}
	
	/**
	 * 地址更新
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/addressUpdate")
	public String addressUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		int id = Integer.valueOf(request.getParameter("id"));
		condMap.put("addressId", id);
		
		
		ApiResponse<AAddress> apiResponse = this.goodsDbService.findOneAAddressService(condMap);
		AAddress address = apiResponse.getDataOneJava();
		
		condMap.clear();
		condMap.put("usersId",address.getUserId());
		
		ApiResponse<AUsers> list = this.usersDbService.findOneUsersService(condMap);
		 request.setAttribute("list", list.toJSON());
		
		request.setAttribute("response", apiResponse.toJSON());
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if ("update".equalsIgnoreCase(operType))
		{
			condMap.clear();
			condMap.put("status", ABlindboxEnum.STATUS_ENABLE.getStatus());
			return "/back/goods/addressUpdate";
		}
		
		return "/back/goods/addressInfo";
	}
	
	/**
	 * 地址更新提交
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/addressUpdateSubmit")
	@ResponseBody
	public String addressUpdateSubmit(HttpServletRequest request,Model model, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("addressId", id);
		
		
		ApiResponse<AAddress> apiResponse = this.goodsDbService.findOneAAddressService(condMap);
		AAddress address = apiResponse.getDataOneJava();
		request.setAttribute("response", apiResponse.toJSON());

		boolean flag = false;
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			
			String addressInformation = request.getParameter("addressInformation");
			String consigneeName = request.getParameter("consigneeName");
			String consigneeNumber = request.getParameter("consigneeNumber");
			String isTheDefaultAddress = request.getParameter("isTheDefaultAddress");
			String postal = request.getParameter("postal");
			
			String status = request.getParameter("status");
			
			/* 赋值 */
			address.setAddressInformation(addressInformation);
			address.setConsigneeName(consigneeName);
			address.setConsigneeNumber(consigneeNumber);
			address.setIsTheDefaultAddress(Byte.valueOf(isTheDefaultAddress));
			address.setPostal(postal);
			address.setStatus(Byte.valueOf(status));
			flag = true;
		}
		

		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if (flag)
		{		 
			 apiResponseDb = this.goodsDbService.updateOneAAddressService(address);
		}

		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		return resultJSON.toJSONString();
	}
}
